<?php
/*
 * 文章管理
 */
class ContentAction extends AdminBaseAction
{
    public function index()
    {
        $this->lists();
    }

    public function lists()
    {
        $m = M('channel');
        $channel = $m->select();
        $channellist = array();
        foreach ($channel as $item) {
            $channellist[$item['id']] = $item['title'];
        }
        $this->assign('channellist', $channellist);

        $m = M('content');
        $items = $m->page($this->_get('p', null, 1) . ',5')->select();

        import("ORG.Util.Page");
        $count = $m->count();
        $Page = new Page($count, 5); // 实例化分页类 传入总记录数和每页显示的记录数
        $show = $Page->show(); // 分页显示输出
        $this->assign('conetnlist', $items);
        $this->assign('page', $show); // 赋值分页输出

        $this->display('list');
    }

    public function add()
    {
        $this->aecommon();
        $this->display('edit');
    }

    public function edit()
    {
        $id = $_GET['id'];

        $m = M('content');
        $item = $m->where('id=' . $id)->find();

        $this->assign('edititem', $item);
        $this->aecommon();
        $this->display('edit');
    }

    protected function aecommon()
    {

        $m = M('channel');
        $items = $m->where('parent_id=0')->select();

        foreach ($items as &$item) {
            $childitems = $m->where('parent_id=' . $item['id'])->select();
            $item['child'] = $childitems;
        }

        $this->assign('channellist', $items);
    }

    public function post_edit()
    {
        $m = M("content");
        $m->create();

        $m2 = M('member');
        $user = $m2->where("name='" . $m->author_name . "'")->find();
        $m->author_uid = $user['id'];
        $count = $m->save();
        if ($count)
            $this->redirect('Content/lists');
        else
            echo 'error';
    }

    public function post_add()
    {
        $m = M("content");
        $m->create();

        $m2 = M('member');
        $user = $m2->where("name='" . $m->author_name . "'")->find();
        $m->author_uid = $user['id'];
        $count = $m->add();

        if ($count)
            $this->redirect('Content/lists');
        else
            echo 'error';
    }
}